package pers.vic.third.utils;

import java.util.List;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 第三方项目读取配置文件的方法
 * @author VIC
 *
 */
public class ThirdConfigurationUtils {

	private static final String file_name = "third-config.properties";
	
	private static Logger logger = LoggerFactory.getLogger(ThirdConfigurationUtils.class);
	
	public static PropertiesConfiguration cfg = null;
	
	static {
		try {
			cfg = new PropertiesConfiguration(file_name);
			//当文件的内容发生改变时，配置对象也会刷新
			cfg.setReloadingStrategy(new FileChangedReloadingStrategy());
		}catch (Exception e) {
			logger.error("读取第三方配置文件错误 {}", ExceptionUtils.getStackTrace(e));
		}
		
	}
	
	// 读String
    public static String getStringValue(String key) {
        return cfg.getString(key);
    }
	// 读int
    public static int getIntValue(String key) {
    	return cfg.getInt(key);
    }
	// 读boolean
    public static boolean getBooleanValue(String key) {
        return cfg.getBoolean(key);
    }
	// 读List
    public static List<?> getListValue(String key) {
        return cfg.getList(key);
    }
    
	// 读数组
    public static String[] getArrayValue(String key) {
        return cfg.getStringArray(key);
    }
    
    public static void main(String[] args) {
		System.out.println(ThirdConfigurationUtils.getStringValue("test.bb"));
		System.out.println(ThirdConfigurationUtils.getIntValue("test.aa"));
		System.out.println(ThirdConfigurationUtils.getListValue("test.cc"));
	}
}
